package cn.xd.base.domain.entity;

import cn.xd.base.basic.type.DictGroupId;
import cn.xd.base.basic.type.DictGroupName;
import cn.xd.base.basic.type.TimeValue;
import cn.xd.base.basic.type.UserId;
import cn.xd.base.domain.bean.command.AddDictGroupCommand;
import cn.xd.base.domain.bean.command.UpdateDictGroupCommand;
import cn.xd.core.ddd.entity.Entity;
import lombok.Data;

import java.util.Date;


/**
 * @author : mirzaLin
 * @data : 2023/11/20
 * @description : 字典
 **/
@Data
public class DictGroup implements Entity<DictGroupId> {
    private DictGroupId id;
    /**
     * 字典名称
     */
    private DictGroupName name;

    private UserId creatorId;

    private TimeValue createTime;

    private UserId updaterId;

    private TimeValue updateTime;

    /**
     * 新增字典分组
     *
     * @param command 新增字典分组
     * @param userId  操作用户id
     * @return 新增字典分组实体
     */
    public static DictGroup add(AddDictGroupCommand command, Long userId) {
        Date now = new Date();
        DictGroup dictGroup = new DictGroup();
        dictGroup.name = DictGroupName.of(command.getName());
        dictGroup.creatorId = UserId.of(userId);
        dictGroup.createTime = TimeValue.of(now);
        dictGroup.updaterId = UserId.of(userId);
        dictGroup.updateTime = TimeValue.of(now);
        return dictGroup;
    }

    /**
     * 更新字典分组
     *
     * @param command 更新字典分组内容
     * @param userId  操作用户id
     */
    public void update(UpdateDictGroupCommand command, Long userId) {
        this.name = DictGroupName.of(command.getName());
        this.updaterId = UserId.of(userId);
        this.updateTime = TimeValue.now();
    }
}
